package Reeks5.VanRansbeeck.Kevin.View;

import Reeks5.VanRansbeeck.Kevin.Util.Settings;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

/*
 * HTML HelpScreen
 */

public class HelpScreen extends JFrame {
    JWindow jw;
    
    URL url;
    
    JEditorPane htmlPane;
    JScrollPane htmlScroll;
    
    SplashButton close;
    
    public HelpScreen() {
        showHelpScreen();
    }
    
    private void showHelpScreen() {
        jw = new JWindow();
        
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        jw.setBounds(100, 100, screen.width - 200, screen.height - 200);
        
        
        try {
            url = ClassLoader.getSystemResource("html/"+Settings.lang.getHelpFile()+".html");
        } catch (Exception e) {
            System.err.println("Couldn't find file: " + url);
        }
        
        try {
            htmlPane = new JEditorPane();
            htmlPane.setEditable(false);
            htmlPane.setPage(url);
            htmlScroll = new JScrollPane(htmlPane);
            htmlScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            htmlScroll.setMinimumSize(new Dimension(400, 400));
            htmlScroll.setPreferredSize(new Dimension(1000,1000));
        } catch (IOException ioe) {
            System.err.println("Attempted to read a bad URL: " + url);
        }
        
        close = new SplashButton(Settings.lang.getClose());
        close.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                if(ae.getSource() == close)
                    jw.dispose();
            }
        });
        
        JPanel htmlPanel = new JPanel(new GridBagLayout());
        JPanel buttonPanel = new JPanel(new FlowLayout());
        
        htmlPanel.add(htmlScroll);
        buttonPanel.add(close);        
          
        jw.getContentPane().add(buttonPanel, BorderLayout.NORTH); 
        jw.getContentPane().add(htmlPanel, BorderLayout.CENTER);   
        
        htmlPane.addHyperlinkListener(new HyperlinkListener() {

            public void hyperlinkUpdate(HyperlinkEvent he) {
                if (he.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    try {
                        htmlPane.setPage(he.getURL());
                    } catch(IOException ioe) {}
                }
            }
        });
                
        jw.setVisible(true);
    }
}